package mcu.market.net;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.net.*;
import org.cyberneko.html.parsers.DOMParser;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.DOMReader;
import org.xml.sax.SAXException;
public class DailyPrice {

	static List<String> slist = new ArrayList<String>();
	static List<String> filter = new ArrayList<String>();
	public static void main(String[] args) throws IOException
	{
		//Compute();
		CreateDataBase();
		//modify();
		//System.out.println("Update Complete !!");
	}
	private static void copyfile(String srFile, String dtFile)
	{
		try
		{
			File f1 = new File(srFile);
			File f2 = new File(dtFile);
			InputStream in = new FileInputStream(f1);
			OutputStream out = new FileOutputStream(f2);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0)
			{
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			System.out.println("File copied.");
		}
		catch(FileNotFoundException ex)
		{
			System.out.println(ex.getMessage() + " in the specified directory.");
			System.exit(0);
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());      
		}
	}
	public static void CreateDataBase() throws IOException
	{
		read_stock_list(slist);
		Date date = new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(int i = 0; i < slist.size(); i++)
		{
			//String file = "E:\\thomas_java\\workspace\\StockDailyPrice\\2_Data\\" + slist.get(i) + ".csv";////
			String file = "C:\\�ѻ��s\\1\\" + slist.get(i) + ".csv";////
			//String outputFile = "E:\\thomas_java\\workspace\\StockDailyPrice\\2_Data_\\" + slist.get(i) + ".csv";////
			String outputFile = "C:\\�ѻ��s\\1_\\" + slist.get(i) + ".csv";////
			File locatedFile = new File(file);
			if(locatedFile.exists())
			{
				BufferedReader reader = new BufferedReader(new FileReader(locatedFile));
				String line = null;
				FileWriter fw = null;
				fw = new FileWriter(outputFile, false);
				String url = "http://hk.finance.yahoo.com/q?s=" + slist.get(i) +".tw";//////
				System.out.println(slist.get(i) + "------------->");
				String latest = get_today_price(url);
				String[] tmp = latest.split("-");
				String out_latest =   dateFormat.format(date) + "," + tmp[1] + "," + tmp[0] + "," + tmp[2];
				int index = 0;
				while((line = reader.readLine()) != null)
				{
					if(index == 0)
					{
						fw.write(out_latest);
						fw.write("\n");
					}
					fw.write(line);
					fw.write("\n");
					fw.flush();
					index ++;
				}
				fw.close();
				copyfile(outputFile,file);
			}

		}
	}

	public static void modify() throws IOException
	{
		read_stock_list(slist);
		System.out.println("----------------->" + slist.size());
		for(int i = 0; i < slist.size(); i++)
		{
			String file = "C:\\�ѻ��s\\2\\" + slist.get(i) + ".csv";////
			String outputFile = "C:\\�ѻ��s\\2_\\" + slist.get(i) + ".csv";
			File locatedFile = new File(file);
			if(locatedFile.exists())
			{
				BufferedReader reader = new BufferedReader(new FileReader(locatedFile));
				String line = null;
				FileWriter fw = null;
				fw = new FileWriter(outputFile, false);
				List<String> t = new ArrayList<String>();
				while((line = reader.readLine()) != null)
				{
					t.add(line);
				}
				System.out.println( slist.get(i) + " ----> " + t.size());
				if(t.size() == 76) t.remove(0);
				for(int k = 0; k < t.size(); k++)
				{
					fw.write(t.get(k) + "");
					fw.write("\n");
					fw.flush();
				}
				fw.close();
				copyfile(outputFile,file);
			}

		}
	}
	public static void Compute()  throws IOException
	{
		read_stock_list(slist);
		Date date = new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String time = dateFormat.format(date);
		String[] tmp_ = time.split("-");
		System.out.println(tmp_[0] + " , " + tmp_[1] + " , " + tmp_[2]);
		int month = Integer.valueOf( tmp_[1]) - 1;
		String des[] = {String.valueOf(month), tmp_[2] , tmp_[0]};
		for(int i = 0; i < slist.size(); i++)
		{
			//2012/01/18 -- 2011/12/17
			// http://ichart.finance.yahoo.com/table.csv?s=2308.TW&a=09&b=1&c=2011&d=00&e=18&f=2012&g=w&ignore=.csv
			String urlstring = "http://ichart.finance.yahoo.com/table.csv?s=" + slist.get(i) + ".TW&a=10&b=1&c=2011&d="+des[0]+"&e="+des[2]+"&f="+des[1]+"&g=d&ignore=.csv";///////////////////
			//String outputFile = "E:\\thomas_java\\workspace\\StockDailyPrice\\1\\" + slist.get(i) + ".csv";////
			/*if(exists(urlstring, i) == false)
			{
				System.out.println(slist.get(i) + " Not Found !!!");
				continue;
			}*/
			filter.add(slist.get(i));
			/*File desFile = new File(outputFile);
			if (desFile.exists()) desFile.delete();
			URL url = new URL(urlstring);
			URLConnection connection = url.openConnection();
			byte[] data = new byte[1];
			BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(desFile));
			while( bufferedInputStream.read(data) != -1) 
				bufferedOutputStream.write(data); 
			bufferedOutputStream.flush();    
			bufferedInputStream.close();
			bufferedOutputStream.close();*/
			System.out.println( slist.get(i) + " �U�� !!");
		}
		System.out.println("��ƤU�� ^_^");
		for(int i = 0; i < filter.size(); i++)
		{
			String outputFile = "E:\\thomas_java\\workspace\\StockDailyPrice\\1_\\" + filter.get(i) + ".csv";/////
			File locatedFile = new File(outputFile);
			List<String> content = new ArrayList<String>();
			String title = "";
			if(locatedFile.exists())
			{
				BufferedReader reader = new BufferedReader(new FileReader(locatedFile));
				String line = null;
				int index = 1;
				while((line = reader.readLine())!=null)
				{
					if(index == 1)  
					{
						title = line;
						index ++;
						continue;
					}
					content.add(line);
					index ++;
				}
			}
			System.out.println("Size : " + content.size());
			if(locatedFile.exists())
			{
				FileWriter fw = null;
				fw = new FileWriter(outputFile, false);
				String url = "http://hk.finance.yahoo.com/q?s=" + filter.get(i) +".tw";//////
				String latest = get_today_price(url);
				String[] tmp = latest.split("-");
				String out_latest = "," + tmp[1] + "," + "," + "," + tmp[0] + "," + tmp[2];
				content.add(0, out_latest);
				content.add(0, title);
				for(int k = 0; k < content.size(); k++)
				{
					fw.write(content.get(k));
					fw.write("\n");
					fw.flush();
				}
				fw.close();
				System.out.println(filter.get(i) + " �s�W���� !!");
			}
		}
	}
	public static  String get_today_price(String url)
	{
		String first_price = "";
		int tag = 0;
		try
		{
			while(true)
			{
				if(tag == 1) break;
				DOMParser parser = new DOMParser();
				parser.parse(url);
				org.w3c.dom.Document document = parser.getDocument();
				DOMReader domReader = new DOMReader();  
				Document doc = domReader.read(document);      
				Node today = doc.selectSingleNode("//SPAN[@class='time_rtq_ticker']/node()");
				List<Node> open = doc.selectNodes("//TD[@class='yfnc_tabledata1']");
				List<Node> quote = doc.selectNodes("//TD[@class='yfnc_tabledata1']/SPAN");
				if(open.size() != 0 && quote.size() != 0)
				{
					System.out.println("Today: " + today.getText());
					System.out.println("Open: " + open.get(1).getText());
					System.out.println("Quote: " + quote.get(quote.size()-1).getText());
					first_price =  today.getText() + "-" + open.get(1).getText() + "-" + quote.get(quote.size()-1).getText().replace(",", "");
					tag = 1;
				}
				else if(open.size() == 0 || quote.size() == 0)
				{

				}
			}
		} catch (SAXException e) {
			System.out.println(e.toString());
		} catch (IOException e) {
			System.out.println(e.toString());
		}
		return first_price;
	}
	public static void read_stock_list(List<String> l) throws IOException
	{
		FileReader fr = new FileReader("StockList1.csv");////////
		BufferedReader bf = new BufferedReader(fr);
		String str;
		while((str = bf.readLine()) != null)
		{
			l.add(str);
		}
	}
	static boolean exists(String URLName, int index) 
	{
		try 
		{
			HttpURLConnection.setFollowRedirects(false);
			HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
			con.setRequestMethod("HEAD");
			return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
		} 
		catch (Exception e) 
		{
			System.out.println(slist.get(index));
			return false;
		}
	}
}
